#include "BondDetailPrcAprismt.h"
#include "BondDetailData.h"
#include <core/time/system_time.h>
#include "../BondUtility.h"
#include <QVBoxLayout>
#include <core/StyleMgr.h>
#include "BondDetailDlg.h"

BondDetailPrcAprismtData::BondDetailPrcAprismtData(BondDetailDlg* p)
	: DetailData(p)
{

}

void BondDetailPrcAprismtData::reqCDCData(const char* bondKey)
{
	QB_CHECK_RETURN_VOID1(bondKey && strlen(bondKey) > 0);
	BondDetailChartData& data = BondDetailCache::instance().getChart(bondKey);
	reqCDCChartData(data);
}

BondDetailPrcAprismtTitle::BondDetailPrcAprismtTitle(QWidget* p /*= nullptr*/)
	: QWidget(p)
{
	setFixedHeight(25);
}

void BondDetailPrcAprismtTitle::setText(QString txt)
{
	m_title = txt;
	update();
}

void BondDetailPrcAprismtTitle::paintEvent(QPaintEvent* event)
{
	QWidget::paintEvent(event);
	QStylePainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.fillRect(rect(), StyleMgr::instance().color("BasicPlate"));
	if (!m_title.isEmpty())
	{
		QFontMetrics fm(font());
		painter.setPen(StyleMgr::instance().color("TextNormal"));
		int titleX = rect().x() + 7;
		int titleWidth = fm.horizontalAdvance(m_title);

		QRect titleRect;
		titleRect.setRect(titleX, rect().y() + 4, titleWidth, rect().height());
		painter.drawText(titleRect, m_title, QTextOption(Qt::AlignLeft));
	}
}

BondDetailPrcAprismt::BondDetailPrcAprismt(QWidget* p, QSharedPointer<DetailData> data)
	: QWidget(p),m_data(data)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

	m_dlg = new BondDetailDlg(this);
	m_prcAprismtdata = new BondDetailPrcAprismtData(m_dlg);

	QVBoxLayout* vLayout = new QVBoxLayout();
	vLayout->setContentsMargins(2, 1, 5, 2);
	vLayout->setSpacing(0);
	m_prcAprismTitle = new BondDetailPrcAprismtTitle();
	m_pAprsmtChart = new BondQuoteCDCChart();
	vLayout->addWidget(m_prcAprismTitle);
	vLayout->addWidget(m_pAprsmtChart);
	setLayout(vLayout);
	m_pAprsmtChart->setTab(0);
	m_pAprsmtChart->createChart();
}

BondDetailPrcAprismt::~BondDetailPrcAprismt()
{
	delete m_prcAprismtdata;
}

void BondDetailPrcAprismt::setReqBondKey(const std::string& strBondKey)
{
	if (strBondKey.compare(m_strReqAprismt))
	{
		m_strReqAprismt = strBondKey;
		if (m_pAprsmtChart)
		{
			bool bRet = CBondContainer::instance().GetBondInfoByCombKey(strBondKey.c_str(), &m_bondInfo);
			if (!bRet)
			{
				log_warning("Can't find bond : %s", strBondKey.c_str());
				return;
			}
			else
			{
				getData();
			}
		}
	}
}

void BondDetailPrcAprismt::getData()
{
	if (!m_strReqAprismt.empty())
	{
		em_NewDetail nReqType = detailModel_CDC_Aprismt_90Day;
		m_prcAprismtdata->clearData();
		m_prcAprismtdata->setCDCType(nReqType);
		m_prcAprismtdata->setBondInfo(m_bondInfo);
		m_prcAprismtdata->reqCDCData(m_strReqAprismt.c_str());

		m_strBondShortName = QString::fromLocal8Bit(m_bondInfo.GetBondShortName()) + " " + tr("中债估值");
		m_prcAprismTitle->setText(m_strBondShortName);


		connect(m_prcAprismtdata, &BondDetailPrcAprismtData::cdcChartChanged,this, &BondDetailPrcAprismt::onBondCDCChanged);
		connect(m_prcAprismtdata, &BondDetailPrcAprismtData::notifyPrimaryAprismtGetMapData, this, &BondDetailPrcAprismt::onBondCDCChanged);//请求过的CDC数据从Map中取
		connect(m_pAprsmtChart->dragBar(), &BaseChartDragBarElement::leftDrag, this, &BondDetailPrcAprismt::onBondCDCLeftDrag);
		connect(m_pAprsmtChart->dragBar(), &BaseChartDragBarElement::rightDrag, this, &BondDetailPrcAprismt::onBondCDCRightDrag);
		connect(m_pAprsmtChart->dragBar(), &BaseChartDragBarElement::centralDrag, this, &BondDetailPrcAprismt::onBondCDCCentralDrag);
	}
}

void BondDetailPrcAprismt::onBondCDCChanged()
{
	if (!m_prcAprismtdata || !m_pAprsmtChart) return;
	//默认为到期收益率
	xCDCKlineDayList_c EstimateCurve;
	FIELDCOPY(EstimateCurve.m_bond_key, m_bondInfo.GetBondKey());
	FIELDCOPY(EstimateCurve.m_listed_market, m_bondInfo.GetListedMarket());

	BondAprismtHistoryData* pData = BondDetailCache::instance().getChart(m_strReqAprismt).m_pAaprismtHisData;
	if (!pData)
	{
		m_pAprsmtChart->loadKLineData(&EstimateCurve);
		return;
	}
	////估值
	std::map<unsigned int, DetailAppraisementPriceData>::iterator itCDC = pData->m_CDCPriceCtner.begin();
	xCDCKlineDayUnit_c cdcDayUnit;
	for (; itCDC != pData->m_CDCPriceCtner.end(); ++itCDC) {
		cdcDayUnit.m_CleanPrice = cdcDayUnit.m_ValueYield = cdcDayUnit.m_DealYield = cdcDayUnit.m_QuoteBid = cdcDayUnit.m_QuoteOfr = cdcDayUnit.m_DealClean = -1000.0f;
		DetailAppraisementPriceData& detailPrice = itCDC->second;
		cdcDayUnit.m_Date = itCDC->first;
		cdcDayUnit.m_ValueYield = detailPrice.m_RecommendAllData.m_Yield;
		cdcDayUnit.m_CleanPrice = detailPrice.m_RecommendAllData.m_CleanPrice;
		EstimateCurve.m_List.push_back(cdcDayUnit);
	}
	m_pAprsmtChart->loadKLineData(&EstimateCurve);
}

void BondDetailPrcAprismt::onBondCDCLeftDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(m_pAprsmtChart);

	m_pAprsmtChart->setLeftDrag(ratio);
	onBondCDCChanged();
}

void BondDetailPrcAprismt::onBondCDCRightDrag(double ratio)
{
	QB_CHECK_RETURN_VOID1(m_pAprsmtChart);

	m_pAprsmtChart->setRightDrag(ratio);
	onBondCDCChanged();
}

void BondDetailPrcAprismt::onBondCDCCentralDrag(double ratio1,double ratio2)
{
	QB_CHECK_RETURN_VOID1(m_pAprsmtChart);

	m_pAprsmtChart->setCentralDrag(ratio1, ratio2);
	onBondCDCChanged();
}
